package com.xiaoq.push.service;
import com.xiaoq.coms.util.BeanToMapUtils;
import com.xiaoq.coms.util.JsonUtil;
import com.xiaoq.push.vo.MessageSenderVo;

import cn.jpush.api.push.model.notification.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import cn.jiguang.common.ClientConfig;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.PushPayload.Builder;
import cn.jpush.api.push.model.audience.Audience;

@Service
public class JdPushService {
	protected static final String APP_KEY ="e6290718d05d0dbbaf18eb39";
    protected static final String MASTER_SECRET = "4ffc01cb32b83d39b70922a6";
//	protected static final String APP_KEY2 ="9a13af8555918b7cbda458b0";
//	protected static final String MASTER_SECRET2 = "d60f504424721086012befb1";
    protected static final String GROUP_PUSH_KEY = "";
    protected static final String GROUP_MASTER_SECRET = "";
    
    @Value("${jd.environment}")
	private String environment;
    
    private static final Logger LOG = LoggerFactory.getLogger(JdPushService.class);
	
    public static void main(String[] args) {
    	MessageSenderVo messageSenderVo = new MessageSenderVo();
    	messageSenderVo.setCornershopId("sites001");
    	messageSenderVo.setMsgCatalogueCode("3");
    	messageSenderVo.setMsgTitle("极光推送");
    	messageSenderVo.setMsgContent("极光推送内容");
    	messageSenderVo.setMesType("mesType");
    	messageSenderVo.setMesTarId("mesTarId");
//    	List<String> regIds = new ArrayList<String>();
//    	regIds.add("161a3797c80f5b5686c");
//    	regIds.add("141fe1da9ea080ff4fc");
    	JdPushService jpush = new JdPushService();
//    	jpush.sendMessageToMultiIOSUsers(messageSenderVo, regIds);
    	jpush.sendMessageToSingleIOSUser(messageSenderVo, "1114a8979284d2913f5",false);
	}

	public  String sendMessageToSingleAndroidUser(MessageSenderVo messageSenderVo,String regId) {
		String title = messageSenderVo.getMsgTitle();
		Map messageMapper = BeanToMapUtils.toMap(messageSenderVo);
		messageMapper.put("pubTime", System.currentTimeMillis()+"");
		ClientConfig clientConfig = ClientConfig.getInstance();
    	JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
		PushPayload payload = PushPayload.newBuilder()
	                .setPlatform(Platform.android())
	                .setAudience(Audience.registrationId(regId))
	                .setNotification(Notification.android(title, title, messageMapper))
	                .build();
		PushResult result = null;
		try {
			result = jpushClient.sendPush(payload);
			LOG.info(result.toString());
		} catch (Exception e) {
			LOG.error(e.getMessage(),e);
		} finally {
			jpushClient.close();
		}
		return JsonUtil.asString(result);
	}
	
	public  String sendMessageToMultiAndroidUsers(MessageSenderVo messageSenderVo,List<String> regIds) {
		String title = messageSenderVo.getMsgTitle();
		Map messageMapper = BeanToMapUtils.toMap(messageSenderVo);
		messageMapper.put("pubTime", System.currentTimeMillis()+"");
		ClientConfig clientConfig = ClientConfig.getInstance();
    	JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
		PushPayload payload = PushPayload.newBuilder()
	                .setPlatform(Platform.android())
	                .setAudience(Audience.registrationId(regIds))
	                .setNotification(Notification.android(title, title, messageMapper))
	                .build();
		PushResult result = null;
		try {
			result = jpushClient.sendPush(payload);
			LOG.info(result.toString());
		} catch (Exception e) {
			LOG.error(e.getMessage(),e);
		}finally {
			jpushClient.close();
		}
		return JsonUtil.asString(result);
	}
	
	public  String sendMessageToSingleIOSUser(MessageSenderVo messageSenderVo,String regId,boolean mutable_content) {
		String title = messageSenderVo.getMsgTitle();
		String content = messageSenderVo.getMsgContent();
		Map messageMapper = BeanToMapUtils.toMap(messageSenderVo);
		messageMapper.put("pubTime", System.currentTimeMillis()+"");
		ClientConfig clientConfig = ClientConfig.getInstance();
		IosAlert alert = IosAlert.newBuilder().setTitleAndBody(title, "", content).build();
				
    	JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
    	PushPayload payload;
    	Builder builder = PushPayload.newBuilder()
	                .setPlatform(Platform.ios())
	                .setAudience(Audience.registrationId(regId))
//	                .setNotification(Notification.ios(title, messageMapper))
	                .setNotification(Notification.newBuilder()
	                        .addPlatformNotification(IosNotification.newBuilder()
	                                .setAlert(alert)
	                                .setBadge(1)
	                                .setSound("happy")
	                                .setContentAvailable(true)  
	                                .setMutableContent(mutable_content)  //通知栏是否显示图片
	                                .addExtra("from", "JPush")
	                                .addExtras(messageMapper)
	                                .build())
	                        .build());
    	LOG.info(environment);
//    	if (!"test".equals(environment)) {
//    		builder.setOptions(Options.newBuilder()
//                    .setApnsProduction(true)
//                    .build());
//    	}
    	payload = builder.build();
		PushResult result = null;
		try {
			result = jpushClient.sendPush(payload);
			LOG.info(result.toString());
		} catch (Exception e) {
			LOG.error(e.getMessage(),e);
		} finally {
			jpushClient.close();
		}
		return JsonUtil.asString(result);
	}
	
	public  String sendMessageToMultiIOSUsers(MessageSenderVo messageSenderVo,List<String> regIds,boolean mutable_content) {
		String title = messageSenderVo.getMsgTitle();
		String content = messageSenderVo.getMsgContent();
		Map messageMapper = BeanToMapUtils.toMap(messageSenderVo);
		messageMapper.put("pubTime", System.currentTimeMillis()+"");
		ClientConfig clientConfig = ClientConfig.getInstance();
		IosAlert alert = IosAlert.newBuilder().setTitleAndBody(title, "", content).build();
    	JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
    	PushPayload payload;
    	Builder builder  =  PushPayload.newBuilder()
	                .setPlatform(Platform.ios())
	                .setAudience(Audience.registrationId(regIds))
//	                .setNotification(Notification.ios(title, messageMapper))
	                .setNotification(Notification.newBuilder()
	                        .addPlatformNotification(IosNotification.newBuilder()
	                                .setAlert(alert)
	                                .setBadge(1)
	                                .setSound("happy")
	                                .setContentAvailable(true)
	                                .setMutableContent(mutable_content)  //通知栏是否显示图片
	                                .addExtra("from", "JPush")
	                                .addExtras(messageMapper)
	                                .build())
	                        .build());
    	LOG.info(environment);
//    	if (!"test".equals(environment)) {
//    		builder.setOptions(Options.newBuilder()
//                    .setApnsProduction(true)
//                    .build());
//    	}
    	payload = builder.build();
    	PushResult result = null;
		try {
			result = jpushClient.sendPush(payload);
			LOG.info(result.toString());
		} catch (Exception e) {
			LOG.error(e.getMessage(),e);
		} finally {
			jpushClient.close();
		}
		return JsonUtil.asString(result);
	}

}
